<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library.Database.Table.Iterator
 */
class Dmf_Database_Table_Iterator_Results implements IteratorAggregate
{
    /**
     * All database-tables
     * 
     * @var array
     */
    private $_tables;
    
    /**
     * Constructor
     * 
     * @param array $tables 
     */
    public function __construct( $tables )
    {
        $this->_tables = $tables;
    }
    
    /**
     * Get first database table
     * 
     * @return Dmf_Database_Table
     */
    public function first()
    {
        return $this->_tables['0'];
    }
    
    /**
     * Get last database table
     * 
     * @return Dmf_Database_Table
     */
    public function last()
    {
        return $this->_tables[ count( $this->_tables ) ];
    }
    
    /**
     * Save all tables 
     */
    public function save()
    {
        foreach( $this->_tables as $table )
        {
            $table->save();
        }
    }
    
    /**
     * Delete all tables 
     */
    public function delete()
    {
        foreach( $this->_tables as $table )
        {
            $table->delete();
        }
    }
    
    /**
     * Get iterator
     * 
     * @return Dmf_Database_Table_Iterator_Iterator 
     */
    public function getIterator()
    {
        return new Dmf_Database_Table_Iterator_Iterator( $this->_tables );
    }
    
    /**
     * Get result as array
     * 
     * @return array
     */
    public function asArray()
    {
        $array = array();
        
        foreach( $this->_tables as $table )
        {
            $array[] = $table;
        }
        
        return $array;
    }
}